<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<!-- Copyright 1997 The Open Group, All Rights Reserved -->
<title>shmat</title>
</head><body bgcolor=white>
<center>
<font size=2>
The Single UNIX &reg; Specification, Version 2<br>
Copyright &copy; 1997 The Open Group

</font></center><hr size=2 noshade>
<h4><a name = "tag_000_008_543">&nbsp;</a>NAME</h4><blockquote>
shmat - shared memory attach operation
</blockquote><h4><a name = "tag_000_008_544">&nbsp;</a>SYNOPSIS</h4><blockquote>
<pre><code>

#include &lt;<a href="sysshm.h.html">sys/shm.h</a>&gt;

void *shmat(int <i>shmid</i>, const void *<i>shmaddr</i>, int <i>shmflg</i>);
</code>
</pre>
</blockquote><h4><a name = "tag_000_008_545">&nbsp;</a>DESCRIPTION</h4><blockquote>
The
<i>shmat()</i>
function
attaches the shared memory segment associated with
the shared memory identifier specified by
<i>shmid</i>
to the address space of the calling process.
The segment is attached at the address
specified by one of the following criteria:
<ul>
<p>
<li>
If
<i>shmaddr</i>
is a null pointer, the segment is attached at
the first available address as selected by the system.
<p>
<li>
If
<i>shmaddr</i>
is not a null pointer and (<i>shmflg</i>&amp;SHM_RND)
is non-zero, the segment is attached at the address given by
(<i>shmaddr</i>-((<i>uintptr_t</i>)<i>shmaddr</i>%SHMLBA))
The character % is the C-language remainder operator.
<p>
<li>
If
<i>shmaddr</i>
is not a null pointer and (<i>shmflg</i>&amp;SHM_RND)
is 0, the segment is attached at the address given by
<i>shmaddr</i>.
<p>
<li>
The segment is attached for reading if (<i>shmflg</i>&amp;SHM_RDONLY)
is non-zero and the calling process has read permission; otherwise, if it is 0
and the calling process has read and write permission, the segment is attached
for reading and writing.
<p>
</ul>
</blockquote><h4><a name = "tag_000_008_546">&nbsp;</a>RETURN VALUE</h4><blockquote>
Upon successful completion,
<i>shmat()</i>
increments the value of
<i>shm_nattch</i>
in the data structure associated with the shared memory ID of the attached
shared memory segment and returns the segment's start address.
<p>
Otherwise, the shared memory segment is not attached,
<i>shmat()</i>
returns -1 and
<i>errno</i>
is set to indicate the error.
</blockquote><h4><a name = "tag_000_008_547">&nbsp;</a>ERRORS</h4><blockquote>
The
<i>shmat()</i>
function will fail if:
<dl compact>

<dt>[EACCES]<dd>
Operation permission is denied to the calling process,
see
<a href="ipc.html">IPC</a>.


<dt>[EINVAL]<dd>
The value of
<i>shmid</i>
is not a valid shared memory identifier;
the
<i>shmaddr</i>
is not a null pointer and the value of
(<i>shmaddr</i>-((<i>ptrdiff_t</i>)<i>shmaddr</i>%SHMLBA))
is an illegal address for attaching shared memory;
or the
<i>shmaddr</i>
is not a null pointer, (<i>shmflg</i>&amp;SHM_RND) is 0 and the value of
<i>shmaddr</i>
is an illegal address for attaching shared memory.

<dt>[EMFILE]<dd>
The number of shared memory segments attached to the
calling process would exceed the system-imposed limit.

<dt>[ENOMEM]<dd>
The available data space is not large enough
to accommodate the shared memory segment.

</dl>
</blockquote><h4><a name = "tag_000_008_548">&nbsp;</a>EXAMPLES</h4><blockquote>
None.
</blockquote><h4><a name = "tag_000_008_549">&nbsp;</a>APPLICATION USAGE</h4><blockquote>
The POSIX Realtime Extension defines alternative interfaces for interprocess communication.
Application developers who need to use IPC should design their
applications so that modules using the IPC routines described in
<a href="ipc.html">IPC</a>
can be easily modified to use the alternative interfaces.
</blockquote><h4><a name = "tag_000_008_550">&nbsp;</a>FUTURE DIRECTIONS</h4><blockquote>
None.
</blockquote><h4><a name = "tag_000_008_551">&nbsp;</a>SEE ALSO</h4><blockquote>
<i><a href="exec.html">exec</a></i>,
<i><a href="exit.html">exit()</a></i>,
<i><a href="fork.html">fork()</a></i>,
<i><a href="shmctl.html">shmctl()</a></i>,
<i><a href="shmdt.html">shmdt()</a></i>,
<i><a href="shmget.html">shmget()</a></i>,
<i><a href="shm_open.html">shm_open()</a></i>,
<i><a href="shm_unlink.html">shm_unlink()</a></i>,
<i><a href="sysshm.h.html">&lt;sys/shm.h&gt;</a></i>,
<a href="ipc.html">IPC</a>.
</blockquote><h4>DERIVATION</h4><blockquote>
Derived from Issue 2 of the SVID.
</blockquote><hr size=2 noshade>
<center><font size=2>
UNIX &reg; is a registered Trademark of The Open Group.<br>
Copyright &copy; 1997 The Open Group
<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]

</font></center><hr size=2 noshade>
</body></html>

